package com.xingkeduo.business.user.service;

import com.xingkeduo.dao.database.DatabaseAccessObject;
import com.xingkeduo.pojo.query.CraftsmanLeavingQO;
import com.xingkeduo.pojo.table.CraftsmanLeavingTO;
import com.xingkeduo.pojo.table.CraftsmanTO;
import com.xingkeduo.util.pojo.exception.DataException;
import com.xingkeduo.util.tool.CommonUtil;
import com.xingkeduo.util.tool.IdentityUtil;
import com.xingkeduo.util.tool.IdentityUtil.Worker;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Iterator;
import java.util.List;

/**
 * 求职者封装类
 *
 * @author 高杰
 * @time 2018年12月25日 下午1:41:24
 * @description
 */
@Component
public class CraftsmanLeavingService {
    private static final String NAMESPACE = CommonUtil.formatNamespace(CraftsmanLeavingTO.class);
    @Resource(name = "mysqlUserMasterDao")
    private DatabaseAccessObject<CraftsmanLeavingTO, CraftsmanLeavingQO> dao;
    private final static String errorMsg = "一线员工离职信息";

    /**
     * 查询最新一条
     *
     * @return
     * @throws Throwable
     * @author 高杰
     * @time 2019年3月13日 下午2:42:05
     */
    public CraftsmanLeavingTO selectLastByCraftsmanId(long craftsmanId) throws Throwable {
        CraftsmanLeavingQO query = new CraftsmanLeavingQO().setCraftsmanId(craftsmanId).setRecordStart(0)
                .setRecordEnd(1);
        List<CraftsmanLeavingTO> leavingList = selectByQuery(query);
        if (CommonUtil.isEmpty(leavingList))
            return null;
        return leavingList.get(0);
    }

    /**
     * 查询
     *
     * @return
     * @throws Throwable
     * @author 高杰
     * @time 2019年3月13日 下午2:42:05
     */
    public List<CraftsmanLeavingTO> selectByQuery(CraftsmanLeavingQO query) throws Throwable {
        return dao.select(NAMESPACE, query);
    }

    /**
     * 新增
     *
     * @param craftsmanLeaving
     * @throws Throwable
     * @author 高杰
     * @time 2019年3月13日 下午2:42:53
     */
    public void insert(CraftsmanLeavingTO craftsmanLeaving) throws Throwable {
        if (dao.insert(NAMESPACE, craftsmanLeaving.initId(IdentityUtil.nextId(Worker.USER))) < 1)
            throw new DataException(DataException.Type.FAIL, errorMsg);
    }

    /**
     * 新增
     *
     * @throws Throwable
     * @author 高杰
     * @time 2018年12月25日 下午2:19:27
     */
    @Transactional
    public void insert(List<CraftsmanLeavingTO> craftsmanJobLeavingList, CraftsmanTO craftsman, long operator)
            throws Throwable {
        for (Iterator<CraftsmanLeavingTO> iterator = craftsmanJobLeavingList.iterator(); iterator.hasNext(); ) {
            iterator.next().setId(craftsman.getId()).setCode(craftsman.getCode()).setCreatedBy(operator)
                    .setUpdatedBy(operator);
        }
        if (dao.insert(NAMESPACE, craftsmanJobLeavingList) < craftsmanJobLeavingList.size())
            throw new DataException(DataException.Type.FAIL, errorMsg);
    }



    @Transactional
    public void update(CraftsmanLeavingTO craftsmanLeavingTO) throws Throwable {
        if (dao.update(NAMESPACE, craftsmanLeavingTO) < 1) {
            throw new DataException(DataException.Type.FAIL, "离职列表更新失败");
        }
    }
}
